package com.xingshang.controller;


import com.xingshang.pojo.Playlist;
import com.xingshang.pojo.Song;
import com.xingshang.pojo.User;
import com.xingshang.service.MusicService;
import com.xingshang.service.PlaylistService;
import com.xingshang.service.UserService;
import com.xingshang.utils.JwtHelper;
import com.xingshang.utils.Result;
import com.xingshang.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PlaylistService playlistService;

    @Autowired
    private JwtHelper jwtHelper;


    @GetMapping("wxlogin")
    public Result wxlogin(@RequestParam String code) {
        System.out.println("code = " + code);
        Result result = userService.wxlogin(code);
        return result;
    }

    @PostMapping("profile/photo")
    public Result uploadPhoto(@RequestParam("file") MultipartFile file, @RequestHeader String token) {
        Result result = userService.uploadPhoto(file, token);
        return result;
    }

    @PostMapping("login")
    public Result login(@RequestBody User user) {
        Result result = userService.login(user);
        return result;
    }

    @GetMapping("info")
    public Result info(@RequestHeader String token) {
        Result result = userService.getUserInfo(token);
        return result;
    }

    @PostMapping("checkUserName")
    public Result checkUserName(String username) {
        Result result = userService.checkUserName(username);
        return result;
    }

    @PostMapping("regist")
    public Result regist(@RequestBody User user) {
        Result result = userService.regist(user);
        return result;
    }




    /**
     * 登陆检查
     * @param token
     * @return
     */
    @GetMapping("checklogin")
    public Result checklogin(@RequestHeader String token) {
        if (jwtHelper.isExpiration(token)) {
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }


    @PutMapping("info")
    public Result updateUserInfo(@RequestBody User user, @RequestHeader String token) {
        System.out.println("user = " + user);
        Long userId = jwtHelper.getUserId(token);
        user.setUserid(Math.toIntExact(userId));
        boolean b = userService.updateById(user);
        if (b) {
            return Result.ok(null);
        } else {
            return Result.build(null, ResultCodeEnum.UPDATE_ERROR);
        }
    }






    @PostMapping("playlist/add")
    public Result addPlaylist(@RequestBody Playlist playlist, @RequestHeader String token) {
        Result result = playlistService.addPlaylist(playlist, token);
        return result;
    }

    @PostMapping("playlist/del")
    public Result delPlaylist(@RequestBody HashMap<String, Integer> data, @RequestHeader String token) {
        System.out.println(data);
        System.out.println("playlistid = " + data.get("playlistid"));
        // 调用服务层进行删除操作
        Result result = playlistService.del(data.get("playlistid"), token);
        return result;
    }


    @GetMapping("playlist")
    public Result get(@RequestHeader String token) {
        Result result = playlistService.getplaylist(token);
        return result;
    }
}
